﻿using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using System.Collections.Generic;

namespace Business
{
    public class Position
    {
        private ISessionFactory _sessionFactory;
        public Position()
        {
            _sessionFactory = DataObjects.Service.SessionFactory.GetInstance().GetNHibernateFactory();
        }
        
        public ArrayList List()
        {
            ArrayList arrRet = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                arrRet = (ArrayList)session.CreateCriteria(typeof(DataObjects.Position)).List();
                tx.Commit();
            }
            return arrRet;
        }

        public DataObjects.Position GetObjectByPK(string posId)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from Position where Position_ID =?").AddEntity(typeof(DataObjects.Position));
                query.SetString(0, posId);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                if (arrTemp.Count > 0)
                {
                    return (DataObjects.Position)arrTemp[0];
                }
                return null;
            }
        }

        public int GetMaxID()
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from Position order by Position_Id desc").AddEntity(typeof(DataObjects.Position));
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                DataObjects.Position oPos;

                if (arrTemp.Count > 0)
                {
                    oPos = (DataObjects.Position)arrTemp[0];
                    if (oPos != null)
                    {
                        return int.Parse(oPos.PositionId);
                    }
                }
                return 0;
            }
        }

        //code insert - update - delete 
        public void Insert(DataObjects.Position oPos)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(oPos);
                tx.Commit();			        
            }
        }
        public void Update(DataObjects.Position oPos)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(oPos);
                tx.Commit();		        
            }
        }
        public void DeleteByPK(string posId)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                DataObjects.Position oPos = (DataObjects.Position)session.Load(typeof(DataObjects.Position), posId);
                session.Delete(oPos);
                tx.Commit();
            }
        }
    }
}
